<?php

defined('BY_JYA') or exit('error');
/*
	游戏营销
*/
$tabs=array(
	array('op'=>'list','title'=>'活动列表'),
	array('op'=>'edit','title'=>'添加/编辑游戏活动'),
);
set_html('tabs',$tabs);
$merchid=plugin('jy_merch')->checklogin();
$return['plugin_icon']=plugin('jy_merch')->session('plugin_icon');
$_RQ['op']=$_RQ['op']?:'list';
$return['types']=$this->canyu_types;
$return['rewardtype']=$this->rewardtype;
$return['get_types']=$this->get_types;
$return['times']=$this->times;
$return['addtimes_types']=$this->addtimes_types;
$return['prizeType']=$this->prizeType;
unset($return['prizeType'][7]);
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'plugin_name'=>'jy_bigwheel','merchid'=>$merchid);	
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['title']){
		$params['title like']=trim($_RQ['title']);
		$return['params']['title']=$_RQ['title'];
	}
	$parameter=array(
		'params'=>$params,
		'limit'=>array(intval($_RQ['page'])?:1,10),
	);
	$orderby=array();
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['num']){
		$psize = $_RQ['num'];
	}else{
		$psize = 10;
	}
	$total='';
	$list=pdo_getslice('jy_weishop_game',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as &$item){
		$item['pluginset']=json_decode($item['pluginset'],true);
		$item['url']=custom_url('jy_bigwheel/game//id='.$item['id'],true);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'title','title'=>'活动标题','type'=>'string'),
			array('name'=>'type','title'=>'参与条件','type'=>'radio','options'=>'types'),
			array('name'=>'time','title'=>'弹窗时机','type'=>'radio','options'=>'times'),
			//array('name'=>'pluginset.rewardtype','title'=>'奖励类型','type'=>'radio','options'=>'rewardtype'),
			array('name'=>'status','title'=>'活动状态','type'=>'switch','set'=>'set'),
			array('name'=>'url','title'=>'活动链接','type'=>'copy'),
			array('name'=>'url','title'=>'活动码','type'=>'ewm'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		)),
		'search'=>array(
			array('type'=>'radio','width'=>15,'name'=>'status','title'=>'活动状态','options'=>'statuss'),
			array('type'=>'string','width'=>15,'name'=>'title','title'=>'活动标题'),
		),
	);	
	$return['htmls']=$htmls;
	$return['statuss']=array(
		'0'=>'关闭',
		'1'=>'启用',
	);
	exi($return);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	$post=get_postdata();
	$status=intval($post['status']);
	pdo_update('jy_weishop_game',array('status'=>$status),array('id'=>$id));
	if($status==0){
		exi('关闭成功！');
	}else{
		exi('开启成功！');
	}
}
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$sum=0;
		if($post['reward']){
			foreach($post['reward'] as &$row){
				$row['rate']=round($row['rate']*1,2);
				$sum+=$row['rate'];
			}
		}
		if($sum && $sum!=100){
			exi('奖品中奖概率之和必须等于100，请检查并重新设置！','error');
		}
		$post['pluginset']['red_shareset']=$post['red_shareset'];
		$data=array(
			'title'=>trim($post['title']),
			'game_id'=>intval($post['game_id']),
			'type'=>intval($post['type']),
			'price'=>round($post['price'],2),
			'goodsid'=>intval($post['goodsid']),
			'time'=>intval($post['time']),
			'status'=>intval($post['status']),
			'sendtitle'=>trim($post['sendtitle']),
			'sendimage'=>trim($post['sendimage']),
			'senddesc'=>trim($post['senddesc']),
			'delredfee'=>trim($post['delredfee']),
			'pluginset'=>ijson_encode($post['pluginset']),
			'addtimes'=>ijson_encode($post['addtimes']),
			'reward'=>ijson_encode($post['reward']),
		);
		$id=intval($post['id']);
		if($id){
			pdo_update('jy_weishop_game',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['createtime']=TIMESTAMP;
			$data['action']='jy_bigwheel/game/';
			$data['plugin_name']='jy_bigwheel';
			$data['merchid']=$merchid;
			pdo_insert('jy_weishop_game',$data);
		}
		exi('操作成功！','','list');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishop_game',array('id'=>$id));
		$item['pluginset']=$item['pluginset']?json_decode($item['pluginset'],true):array('rewardtype'=>0);
		$item['addtimes']=$item['addtimes']?json_decode($item['addtimes'],true):array();
		$item['red_shareset']=$item['pluginset']['red_shareset']?:array();
	}else{
		$item=array('title'=>'','content'=>array('goodsid'=>''));
		$item['pluginset']=array();
		$item['red_shareset']=array();
	}
	$item['reward']=$item['reward']?json_decode($item['reward'],true):array();
	$item['sendtitle']=$item['sendtitle']?$item['sendtitle']:'[nickname]:恭喜您，获得一个参与奖励活动机会';
	$item['sendimage']=$item['sendimage']?$item['sendimage']:'../core/resource/images/h5game/bigwheel.png';
	$item['senddesc']=$item['senddesc']?$item['senddesc']:'活动有时间限制，请及时参与！';
	$isfenxiao=$this->plugin_exists('jy_commission');
	if($isfenxiao){
		$item['isfenxiao']=1;
		$return['fenxiao_levels']=pdo_getall('jy_commission_level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
		$return['fenxiao_levels']['0']=array('id'=>0,'title'=>'默认等级');
	}else{
		unset($return['addtimes_types'][8]);
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基础设置',
				'list'=>array(
					array('name'=>'title','title'=>'活动标题','type'=>'string'),
					array('name'=>'pluginset.total','title'=>'活动总人次限制','type'=>'string'),
					array('name'=>'pluginset.red_min','title'=>'活动介绍','type'=>'text'),
					array('type'=>'string','name'=>'sendtitle','title'=>'推送标题','help'=>'变量 [nickname] 会员昵称'),
					array('type'=>'image','name'=>'sendimage','title'=>'推送封面'),
					array('type'=>'string','name'=>'senddesc','title'=>'推送说明','help'=>'变量 [nickname] 会员昵称'),	
					array('name'=>'status','title'=>'是否开启','type'=>'switch'),
			)),
			/*array(
				'title'=>'次数奖励',
				'list'=>array(
					array(
							'type'=>'array',
							'name'=>'addtimes',
							'title'=>'次数奖励',
							'add_tip'=>'添加一个奖励条件',
								'list'=>array(
									array('name'=>'type','title'=>'奖励条件','type'=>'radio','options'=>'addtimes_types'),
									array('type'=>'string','name'=>'num','title'=>'奖励次数'),
								),
								'details'=>array(
									array('name'=>'type','title'=>'奖励条件','type'=>'radio','options'=>'addtimes_types'),
									array('type'=>'string','name'=>'num','title'=>'奖励次数'),
									array('name'=>'price','title'=>'最低订单金额','type'=>'string','help'=>'','show'=>array('type'=>1)),
									
								)
					),
			)),*/
			array(
				'title'=>'参与条件设置',
				'list'=>array(
					array('name'=>'type','title'=>'奖励条件','type'=>'radio','options'=>'addtimes_types'),
					array('name'=>'pluginset.fenxiao_level','title'=>'指定分销商等级','type'=>'select','options'=>'fenxiao_levels','show'=>array('type'=>8)),
					array('name'=>'goodsid','title'=>'选择商品','type'=>'choose','help'=>'','action'=>'jy_weishop/goods.get_goods/','show'=>array('type'=>2)),
					array('name'=>'price','title'=>'最低订单金额','type'=>'string','help'=>'','show'=>array('type'=>1)),
					array('type'=>'unitstring','unit'=>'元','title'=>'每次抽奖消耗N元','name'=>'delredfee','show'=>array('type'=>9)),
					array('name'=>'time','title'=>'弹窗时机','type'=>'radio','options'=>'times','show'=>array('type !='=>3),'help'=>'弹窗，提醒参与活动的时机！'),
			)),
			array(
				'title'=>'奖项设置',
				'list'=>array(
					array('name'=>'pluginset.sale_num','title'=>'邀请N个好友后才能领奖？','type'=>'unitstring','unit'=>'人','help'=>'默认不开启，设置后分享了多少人后才能领奖！'),
					array('type'=>'tip','title'=>'[<red>温馨提示</red>]：奖品的图片必须是正方形，否则一些地方显示会异常！<br>1、如果已中数量，已中金额大于等于奖品总数量/红包总金额，会提示库存不足！<br>2、如果所有有概率的，库存都不足，就会显示库存不足（概率0的库存再多也没用）。'),
					array(
					'type'=>'array',
					'name'=>'reward',
					'add_tip'=>'添加一个奖项',
						'list'=>array(
							array('type'=>'string','title'=>'奖品名称','name'=>'title'),
							array('type'=>'radio','title'=>'奖品类型','name'=>'type','help'=>'','options'=>'prizeType'),
							array('type'=>'string','title'=>'红包总金额','name'=>'redall','show'=>array('type'=>'7')),
							array('type'=>'string','title'=>'单个红包最小金额','name'=>'redmin','show'=>array('type'=>'7')),
							array('type'=>'string','title'=>'单个红包最大金额','name'=>'rexmax','show'=>array('type'=>'7')),
							array('type'=>'string','title'=>'奖励数量','name'=>'jifen','show'=>array('type'=>array(2,3,4,5,6))),
							array('type'=>'string','title'=>'奖品等级','name'=>'level'),
							array('type'=>'string','title'=>'奖品总数量','name'=>'num'),
							array('type'=>'string','title'=>'已中数量','name'=>'isgetnum'),
							array('type'=>'string','title'=>'已中红包额','name'=>'isgetfee'),
							array('type'=>'string','title'=>'中奖概率','name'=>'rate'),
							array('type'=>'image','title'=>'奖品图片','name'=>'image','help'=>'温馨提示：<red>建议正方形图片</red>，否则有些游戏会显示异常！'),
						),
							'details'=>array(
								array('type'=>'string','title'=>'奖品名称','name'=>'title'),
								array('type'=>'radio','title'=>'奖品类型','name'=>'type','help'=>'支付参数--虚拟币--开启更多币种奖励','options'=>'prizeType'),
								array('type'=>'string','title'=>'红包总金额','name'=>'redall','show'=>array('type'=>'7')),
								array('type'=>'string','title'=>'单个红包最小金额','name'=>'redmin','show'=>array('type'=>'7')),
								array('type'=>'string','title'=>'单个红包最大金额','name'=>'rexmax','show'=>array('type'=>'7')),
								array('type'=>'string','title'=>'奖励数量','name'=>'jifen','show'=>array('type'=>array(2,3,4,5,6))),
								array('type'=>'string','title'=>'奖品等级','name'=>'level'),
								array('type'=>'string','title'=>'奖品总数量','name'=>'num'),
								array('type'=>'unitstring','unit'=>'%','title'=>'中奖概率','name'=>'rate'),
								array('type'=>'image','title'=>'奖品图片','name'=>'image','help'=>'温馨提示：<red>建议正方形图片</red>，否则有些游戏会显示异常！'),
								
							)
					),
			)),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_weishop_game',array('id'=>$id));	
	}
	if(!$id){
		exi('请选择活动删除！','error');
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_weishop_game',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
